/**
 * JAVACC DEMO 1.0
 * @copy right iussoft company All rights reserved.
 * @Package com.apache.uct.service.plugins  
 */
package com.apache.uct.service.plugins;

import com.apache.api.manager.BaseManager;
import com.apache.api.manager.ProxyManager;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.api.vo.ResultMsg;
import com.apache.cache.util.Validator;
import com.apache.database.constant.SystemTools;
import com.apache.tools.ClassToolsUtil;
import com.apache.tools.DateUtils;
import com.apache.tools.StrUtil;
import com.apache.uct.common.entity.Dept;
import com.apache.uct.common.entity.Org;
import com.apache.uct.common.entity.User;
import com.apache.uct.manager.DeptManager;
import com.apache.uct.manager.OrgManager;
import com.apache.uct.manager.UserManager;

import java.util.Date;

/**
 * description:  用户管理插件
 * @author dyhou 创建时间：2016-8-18  
 */
public class UserActionPluginImpl extends AbstractSuperPlugin {

	private UserManager userManager;
	private OrgManager orgManager;
	private DeptManager deptManager;
	private ProxyManager uctProxyManager;

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.apache.api.manager.PluginConnector#execute(com.apache.api.vo.ParamsVo)
	 */
	public Object execute(ParamsVo param) throws Exception {
		ResultEntity result = new ResultEntity();
		result.setResult("true");
		String method = String.valueOf(param.getParams("method"));
		if ("save".equals(method)) {
			result = save(result, param);
		} else if ("delete".equals(method)) {
			result = delete(result, param);
		} else if ("gc".equalsIgnoreCase(method)) {//用户回收恢复

		} else if ("backList".equalsIgnoreCase(method)) {//回收站列表

		}
		return result;
	}

	private ResultEntity save(ResultEntity result, ParamsVo param) {
		ParamsVo<User> vo = new ParamsVo<User>();
		User user = new User();
		user = (User) ClassToolsUtil.getInstance().convert2Bean(param.getParams(), user);
		long times = DateUtils.toLong(new Date());
		buildUserInfo(user, param, String.valueOf(param.getParams("nowOrgId")));
		String md5Pass = String.valueOf(param.getParams("md5Pass"));
		if (Validator.isNotNull(user.getUserId())) {//修改用户信息
			user.setUpdateTime(times);
			if (Validator.isNotNull(md5Pass)) {
				user.setUserPass(md5Pass.toLowerCase());
			}
		} else {//添加用户信息
			if (Validator.isNotNull(md5Pass)) {
				user.setUserPass(md5Pass.toLowerCase());
			}
			user.setCreateUser(user.getUserEname());
			user.setSysEname(Validator.getDefaultStr(user.getSysEname(),"uct"));
			user.setUpdateCount(0);
			user.setCreateTime(times);
			vo.setKey("checkUserEname");
			if (Validator.isNull(user.getDelStatus())) {
				user.setDelStatus("0");//默认机构用户
			} else {
				if (!"1".equals(StrUtil.doNull(String.valueOf(param.getParams("nowSysUser")), "0"))) {//不是系统用户无权建系统用户
					user.setDelStatus(StrUtil.doNull(user.getDelStatus(), "2"));//默认机构用户
				}
			}
			if (Validator.isEmpty(user.getUserStatus())) {
				user.setUserStatus(1);//默认用户启用状态
			}
			vo.setObj(user);
			String msg = String.valueOf(userManager.execute(vo));
			if (Validator.isNotNull(msg)) {
				result.setMessage(msg);
				return result;
			}
		}
		vo.setObj(user);
		ResultMsg msg = super.save(vo, user.getUserId());
		if ("T".equals(msg.getFlag())) {
			result.setEntity(msg.getFlag());
			result.setMessage(msg.getFlag());
		} else {
			result.setMessage(msg.getFlag());
		}
		return result;
	}

	private ResultEntity delete(ResultEntity result, ParamsVo param) {
		ParamsVo<User> vo = new ParamsVo<User>();
		String id = String.valueOf(param.getParams("userId"));
		if (Validator.isNotNull(id)) {
			//			String isDel = Validator.getDefaultStr(String.valueOf(param.getParams("isDelete")), "false");
			//			if ("false".equals(isDel)) {
			//				vo.setInfoId(id);
			//				vo.setParams("isDelete", "false");
			//				boolean mark = userManager.deleteInfo(vo);
			//				if (!mark) {
			//					result.setMessage("删除失败！");
			//					return result;
			//				}
			//			} else {
			vo.setInfoId(id);
			vo.setKey("deleteOrg");
			vo.setParams("infoType", "user");
			String msg = String.valueOf(uctProxyManager.doInvoke(vo));
			if (Validator.isNotNull(msg)) {
				result.setMessage("删除失败！");
				return result;
			}
			//			}
		} else {
			result.setMessage("请选择要删除的用户！");
		}
		result.setEntity("T");
		result.setMessage("删除用户成功！");
		return result;
	}

	/**
	 * description:  组织用户信息
	 */
	private void buildUserInfo(User item, ParamsVo param, String orgId) {
		if (Validator.isNull(item.getUserId())) {
			if (Validator.isNull(item.getOrgId())) {
				item.setOrgId(orgId);
			}
		}
		///进行修改操作(开启授权的机构用户不可以修改用户所在机构)
		if (Validator.isNotNull(item.getUserId())
				&& ("1".equals(StrUtil.doNull(String.valueOf(param.getParams("nowSysUser")), "0")) || !"1"
						.equals(SystemTools.getInstance().getValue("org_act_switch")))) {
			if (Validator.isNull(item.getOrgId())) {
				item.setOrgId(String.valueOf(param.getParams("oldOrgId")));
			}
		}
		ParamsVo ovo = new ParamsVo();
		if (Validator.isNotNull(item.getOrgId())) {
			ovo.setInfoId(item.getOrgId());
			Org org = (Org) orgManager.getInfoById(ovo);
			item.setOrgEname(org.getOrgEname());
		}
		if (Validator.isNotNull(item.getUserId()) && Validator.isNull(item.getDeptId())) {
			item.setDeptId(String.valueOf(param.getParams("oldDeptId")));
		}
		if (Validator.isNotNull(item.getDeptId())) {
			ovo.setInfoId(item.getDeptId());
			Dept dept = (Dept) deptManager.getInfoById(ovo);
			item.setDeptName(dept.getDeptCname());
		}
	}

	protected BaseManager getBaseManager() {
		return userManager;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	public void setOrgManager(OrgManager orgManager) {
		this.orgManager = orgManager;
	}

	public void setDeptManager(DeptManager deptManager) {
		this.deptManager = deptManager;
	}

	public void setUctProxyManager(ProxyManager uctProxyManager) {
		this.uctProxyManager = uctProxyManager;
	}

}
